$(document).ready(function() {
	$('#phone').focus(function() {
		$('#phone-img').empty();
		$('#login-errinfo').html('');
	});
	
	$('#password').focus(function() {
		$('#password-img').empty();
		$('#login-errinfo').html('');
	});
	
	$('#valicode').focus(function() {
		$('#login-errinfo').html('');
	});
	
	//----- 手机号验证 -----
	$('#phone').blur(function() {
		var phone = $.trim($('#phone').val());
		
		if ( phone ) {
			$.ajax({
	            type: "POST",
	            url: "valiPhone.do",
	            data: {phone:phone},
	            async: true,
	            cache: false,
	            complete: function() {},
	            success: function(msg) {
	                if (msg.result == true) {
	                	$('#phone-img').empty().append('<img src="images/onCorrect.gif">');
	                }
	                else {
	                	$('#phone-img').empty().append('<img src="images/onError.gif">');
	                }
	            },
	            error: function() {
	                alert('手机号验证异常');
	            }
	        });
		}
	});
	
	//----- 密码验证 -----
	$('#password').blur(function() {
		var phone = $.trim($('#phone').val());
		var password = $.trim($('#password').val());
		
		if (phone && password) {
			$.ajax({
	            type: "POST",
	            url: "valiPassword.do",
	            data: {phone:phone, password:password},
	            async: true,
	            cache: false,
	            complete: function() {},
	            success: function(msg) {
	                if (msg.result == true) {
	                	$('#password-img').empty().append('<img src="images/onCorrect.gif">');
	                }
	                else {
	                	$('#password-img').empty().append('<img src="images/onError.gif">');
	                }
	            },
	            error: function() {
	                alert('密码验证异常');
	            }
	        });
		}
	});
	
	//----- 登录 -----
	$('#login').click(function() {
		$('#login').val('登录中...');
		
		var phone = $.trim($('#phone').val());
        var password = $.trim($('#password').val());

        if (!phone || !password) {
            $('#login-errinfo').html('登录帐号或登录密码不能为空');
			$('#login').val('登  录');
			
            return;
        }
        
        var valicode = $.trim($('#valicode').val());
        if ( !valicode ) {
        	$('#login-errinfo').html('验证码不能为空');
			$('#login').val('登  录');
			
            return;
        }
        
        var reg = new RegExp("^[0-9]*$");
        if(!reg.test(valicode)) {
        	$('#login-errinfo').html('验证码只能为数字');
        	$('#login').val('登  录');
        	
            return;
        }
        
        $.ajax({
            type: "POST",
            url: "webLogin.do",
            data: {phone:phone, password:password, valicode:valicode},
            async: true,
            cache: false,
            complete: function() {},
            success: function(msg) {
            	if (msg.result == true) {
            		if (msg.shoujih == '13735520642') {
            			 location.href = "data.jspDo";
            		} else {
            			location.href = "queryVIP.jspDo?shanghuid=" + msg.shanghuid;
            		}
            	} else {
                	$('#login-errinfo').html(msg.msg);
                	$('#login').val('登  录');
        			
                    return;
                }
            },
            error: function() {
                $('#login-errinfo').html('系统异常, 请重试');
				$('#login').val('登  录');
            }
        });
	});
	
	//----- 回车 -----
	$('#login-outter').keydown(function(event) {
		if ("13" == event.keyCode) {
			$('#login').val('登录中...');
		
			var phone = $.trim($('#phone').val());
	        var password = $.trim($('#password').val());
	
	        if (!phone || !password) {
	            $('#login-errinfo').html('登录帐号或登录密码不能为空');
				$('#login').val('登  录');
				
	            return;
	        }
	        
	        var valicode = $.trim($('#valicode').val());
	        if ( !valicode ) {
	        	$('#login-errinfo').html('验证码不能为空');
				$('#login').val('登  录');
				
	            return;
	        }
	        
	        var reg = new RegExp("^[0-9]*$");
	        if(!reg.test(valicode)) {
	        	$('#login-errinfo').html('验证码只能为数字');
	        	$('#login').val('登  录');
	        	
	            return;
	        }

	        $.ajax({
	            type: "POST",
	            url: "webLogin.do",
	            data: {phone:phone, password:password, valicode:valicode},
	            async: true,
	            cache: false,
	            complete: function() {},
	            success: function(msg) {
	                if (msg.result == true) {
	                	if (msg.shoujih == '13735520642') {
	                		location.href = "data.jspDo";
	                	} else {
	                		location.href = "queryVIP.jspDo?shanghuid=" + msg.shanghuid;
	                	}
	                }
	                else {
	                	$('#login-errinfo').html(msg.msg);
	                	$('#login').val('登  录');
	        			
	                    return;
	                }
	            },
	            error: function() {
	                $('#login-errinfo').html('系统异常, 请重试');
					$('#login').val('登  录');
	            }
	        });
		}
	});
});